package br.ufpe.cin.rva;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
                
/** Simple example illustrating the use of TesteSoundButton, especially
 *  the ability to add Borders.
 *  1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 */

public class TesteSoundButton extends JFrame {

	public Button beepButton = new Button("Beep");
	
  public static void main(String[] args) {
    new TesteSoundButton();
  }

  public TesteSoundButton() {
    super("Using TesteSoundButton with Borders");
    Container content = getContentPane();
    beepButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			MakeSound2 makeSound = new MakeSound2();
			makeSound.playSound("D:/DESENVOLVIMENTO/workspaceUFPE/PianoCrystal/res/beep.wav");
			
		}
	});
	content.add(beepButton);
    JPanel drawingArea = new JPanel();
    // Preferred height is irrelevant, since using WEST region
    drawingArea.setPreferredSize(new Dimension(0, 0));
    drawingArea.setBorder
             (BorderFactory.createLineBorder (Color.blue, 2));
    drawingArea.setBackground(Color.white);
    content.add(drawingArea, BorderLayout.WEST);
    pack();
    setVisible(true);
  }
}

class MakeSound2 {

    private final int BUFFER_SIZE = 128000;
    private File soundFile;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;

    /**
     * @param filename the name of the file that is going to be played
     */
    public void playSound(String filename){

        String strFilename = filename;

        try {
            soundFile = new File(strFilename);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        try {
            audioStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try {
            sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        sourceLine.start();

        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) {
            try {
                nBytesRead = audioStream.read(abData, 0, abData.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nBytesRead >= 0) {
                @SuppressWarnings("unused")
                int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
            }
        }

        sourceLine.drain();
        sourceLine.close();
        
    }
}